<?php
class core extends wenyan {
	public $db;
	public $ezr; //分页
	public $tbp;
	public $admin_name;
	public $admin_id;
	public $smarty;
	public $domain;
	public $act, $app;
	public $notify; //消息推送

	public function __construct() {
		parent::__construct();
		global $db, $ezr, $tbp,$smarty,$domain;
		global $notify;
		global $act, $app;
		$this->db = $db;
		$this->ezr = $ezr;
		$this->smarty = $smarty;
		$this->domain = $domain;
		$this->tbp = $tbp;
		$this->app = $app;
		$this->act = $act;
		//消息推送
		$this->notify = $notify;	
		if (in_array($act, array("login"))) {
			$this->admin_id = 0;
			$this->admin_name = '';
		} else {
			$this->check_login();
		}
		$this->_handle_log();
		//菜单
		$this->_menu();
	}

	//是否登录
	public function _check_login() {
		if (isset($_SESSION['admin']) 
			&& is_object($_SESSION['admin']) 
			&& isset($_SESSION['admin']->id) 
			&& intval($_SESSION['admin']->id)>0 
		) {
			$this->admin_id = $_SESSION['admin']->id;
			$this->admin_name = $_SESSION['admin']->name;
			return true;
		} else {
			return false;
		}
	}

	//对是否登录函数的封装，如果登录了，则返回true，
	//否则，返回错误信息：-100，APP需检查此返回值，判断是否需要重新登录
	public function check_login(){
		if ($this->_check_login()) {
			return true;
		} else {
			//error('请登录',-100);
			$_SESSION['gourl'] = $_SERVER['REQUEST_URI'];
			header("Location: index.php?act=login");
		}
	}

	//显示
	public function display($tpl = '', $data=array()) {
		global $json;
		if ($tpl) {
			$template_file = $tpl;
		} else {
			$template_file = $this->app.".".$this->act.".html";
			if (!file_exists(TEMPLATE_PATH . '/' . $template_file)) {
				die("Template does not exists.");
			}
		}
		$this->smarty->assign("app", $this->app);
		$this->smarty->assign("act", $this->act);
		$this->smarty->assign("data", $data);	
		if (isset($_SESSION['admin']) && is_object($_SESSION['admin'])) {
			$this->smarty->assign("admin", $_SESSION['admin']);
		}

		if($this->app == 'home' || $this->act == 'info' || $this->act == 'password'){
			$this->smarty->display($template_file);
		}else{
			
			$sql = "SELECT id FROM jf_admin_permission WHERE app = '{$this->app}' AND act = '{$this->act}' 
					AND admin_id = '{$this->admin_id}'";
			$permission = $this->db->get_var($sql);
			// var_dump($permission);exit;
			if($permission || $this->admin_id < 2){
				$this->smarty->display($template_file);	
			}else{
				error('你没有权限，请联系超级管理员！');
				$this->smarty->display('home.index.html');
			}	
		}
		
		exit;
	}


	//设置分页的参数之类的
	public function setpages($data = array()) {
		$BRSR = (isset($_GET['BRSR']) && trim($_GET['BRSR'])) ? trim($_GET['BRSR']) : '';
		$this->smarty->assign('BRSR',$BRSR);
		//$this->ezr->num_results_per_page=10;
		//$this->ezr->num_browse_links = 10;
		$this->ezr->set_qs_val('app',$this->app);
		$this->ezr->set_qs_val('act',$this->act);
		foreach ($data as $key=>$value) {
			$this->ezr->set_qs_val($key, $value);
			$this->smarty->assign($key, $value);
		}
	}

	//trigger error, exit
	public function error($msg, $msg_type=-1, $error=1) {
		global $json;
		$json['error'] = $error;
		$json['msg_type'] = $msg_type;
		$json['msg'] = $msg;
		$this->smarty->assign("error", $json);	
		$this->display('error.html');
	}

	private function _handle_log() {
		$log = m("log");
		$log->create();
	}

	public function send_all_brocast($ticker,$title,$text,$activity,$extra,$type_id,$board_id){
		$this->notify->sendBroadcast($ticker,$title,$text,$activity,$extra,$type_id,$board_id);
	}
	public function send_all_listcast($ticker, $title, $text, $activity, $extra, $android_device_tokens,$ios_device_tokens, $push_type,$board_id){
		$this->notify->sendListcast($ticker, $title, $text, $activity, $extra, $android_device_tokens,$ios_device_tokens, $push_type,$board_id);
	}
	//消息推送与短息，先保存到数据库，再发送
	public function send_notify_sms($message, $member_id=-1, $send_sms=false, $sms_content=array(), $template_id=128241, $goto_target='') {
		$mobile = $udid = "";
		if ($member_id>0) {
			$sql = "SELECT udid, mobile FROM jf_member WHERE id='{$member_id}'";
			$member = $this->db->get_row($sql);
			if ($member) {
				$udid = $member->udid;
				$mobile = $member->mobile;
			}
		} else {
			$member_id = $this->member_id;
			$udid = $_SESSION['member']->udid;
			$mobile = $_SESSION['member']->mobile;
		}
		//写入消息表
		$message_type = "";
		if (strpos($message, "商品")) {
			$message_type = "goods";
		} elseif (strpos($message, "店铺")) {
			$message_type = "shop";
		} elseif (strpos($message, "订单")) {
			$message_type = "order";
		} else {
			$message_type = "html5";
		}
		$sql = "INSERT INTO jf_message(member_id, content, add_time, readed, message_type, goto_target) 
			VALUES('{$member_id}', '{$message}', '".time()."', '0', '{$message_type}', '{$goto_target}')";
		$this->db->query($sql);
		//发送推送
		$this->notify->send($udid, $message, "", "", array("type"=>"message"));
		//发送短信
		if ($send_sms && $mobile) {
			send_sms($mobile, $sms_content, $template_id);
		}
	}
	private function _menu(){
		$nav = $this->menu($this->admin_id);
		// print_r($menu);exit;
		$this->smarty->assign('nav', $nav);
	}
	public function menu($admin_id=""){
		$sql = "SELECT * FROM jf_admin_menu WHERE admin_id = '{$admin_id}'";
		$menu = $this->db->get_results($sql);
		$sql = "SELECT * FROM jf_gm_menu WHERE parent_id = 0 ORDER BY id ASC";
		$pa = $this->db->get_results($sql);
		$sql = "SELECT * FROM jf_gm_menu WHERE parent_id > 0 ORDER BY id ASC";
		$pass = $this->db->get_results($sql);
		foreach ($pa as $k => $v) {
			$ch = 'a'.$k;
			$ch =new stdClass();
			if($menu){
				foreach ($menu as $ks => $va) {
					//判断是否选中
					if($va->menu_id == $v->id || $admin_id == 1){
						$pa[$k]->checked = 'checked';
					}

					foreach ($pass as $key => $val) {
						//判断是否选中
						if($va->menu_id == $val->id || $admin_id == 1){
							$pass[$key]->checked = 'checked';
						}
						if($v->id == $val->parent_id){
							$ch->$key = $pass[$key];
						}
					}
				}
			}else{//如果都没有权限
				foreach ($pass as $key => $val) {
						//判断是否选中
						if($v->id == $val->parent_id){
							$ch->$key = $pass[$key];
						}
					}
			}
			$pa[$k]->sub = $ch;
		}
		return $pa;
		
	}
}
